//
//  HCStatusCell.swift
//  新浪微博
//
//  Created by fanhoucheng on 16/9/27.
//  Copyright © 2016年 fanhoucheng. All rights reserved.
//

import UIKit
import SDWebImage

let commonMargin: CGFloat = 8
let itemWH:CGFloat = (UIScreen.main.bounds.size.width - 2*commonMargin - 2 * (pictureMargin)) / 3
let pictureMargin: CGFloat = 5
class HCStatusCell: UITableViewCell {
    //用户头像
    @IBOutlet weak var iconView: UIImageView!
    //用户名
    @IBOutlet weak var nameLable: UILabel!
    //用户等级
    @IBOutlet weak var menberImage: UIImageView!
    //微博创建时间
    @IBOutlet weak var timeLabel: UILabel!
    //微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    //微博正文
    @IBOutlet weak var contentLabel: UILabel!
    //微博认证类型
    @IBOutlet weak var avatarImage: UIImageView!
    
    //collectionView的宽度
    @IBOutlet weak var pictureView: HCPictureView!
    
    //collectionView的高度
    @IBOutlet weak var pictureWidth: NSLayoutConstraint!
    
    @IBOutlet weak var picktureHeight: NSLayoutConstraint!
    
    @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
    
    //配图视图顶部约束
    @IBOutlet weak var pictureViewTopCons: NSLayoutConstraint!
    //评论
    @IBOutlet weak var commentBtn: UIButton!
    //转发
    @IBOutlet weak var repostBtn: UIButton!
    //点赞
    @IBOutlet weak var ohYeah: UIButton!
    
    @IBOutlet weak var retweetedLabel: UILabel!
    
    @IBOutlet weak var toolBarView: UIView!
    
    var viewModel: HCStatusViewModel? {
        didSet {
            iconView.sd_setImage(with: viewModel?.iconURL)
            timeLabel.text = viewModel?.status?.created_at
            sourceLabel.text = viewModel?.status?.source
            contentLabel.text = viewModel?.status?.text
            avatarImage.image = viewModel?.avatarImage
            menberImage.image = viewModel?.memberImage
            nameLable.text = viewModel?.status?.user?.name
            
            //配图视图的大小
            let count = viewModel?.pic_urls?.count ?? 0
            let pSize = changePictureViewSize(count: count)
            
            //更新约束
            pictureWidth.constant = pSize.width
            picktureHeight.constant = pSize.height
            
            pictureView.picktureInfo = viewModel?.pic_urls
            pictureViewTopCons.constant = count == 0 ? 0 : commonMargin
            
            commentBtn.setTitle(viewModel?.comment_text, for: .normal)
            repostBtn.setTitle(viewModel?.repost_text, for: .normal)
            ohYeah.setTitle(viewModel?.ohYeahText, for: .normal)
            
            //
            retweetedLabel?.text = viewModel?.status?.retweeted_status?.text
        }
    }
    
    func rowHeight(viewModel: HCStatusViewModel) -> CGFloat {
        self.viewModel = viewModel
        self.layoutIfNeeded()
        return toolBarView.frame.maxY
    }
    
    //用图片的张数来确定图片视图的大小
    func changePictureViewSize(count: Int) -> CGSize {
        
        //设置图片之间的间距
        
        //张数 0
        if count == 0 {
            return CGSize.zero
        }
        
        // item的宽度高 = (屏幕宽度-2*commonMargin - 2 *(pictureMargin)) / 3
        
        //张数 4
        if count == 4 {
            
            //获取大小
            let collectionWH = 2 * itemWH + pictureMargin
            return CGSize(width: collectionWH, height: collectionWH)
        }
        
        //张数 1,2,3,5,6,7,8,9
        //先判断几行,
        let rowCount = CGFloat((count - 1) / 3 + 1)
        let height = rowCount * itemWH + (rowCount - 1) * pictureMargin
        return CGSize(width: UIScreen.main.bounds.size.width - 2*commonMargin, height: height)

    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //self.contentView.backgroundColor = UIColor.blue
        
        contentLabel.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width - 2 * commonMargin
        retweetedLabel?.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width - 2 * commonMargin
        
        flowLayout.itemSize = CGSize(width: itemWH, height: itemWH)
        flowLayout.minimumLineSpacing = pictureMargin
        flowLayout.minimumInteritemSpacing = pictureMargin
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
